package com.yidu.hbatsft.action;

import com.yidu.hbatsft.domain.AtsAttachAudit;
import com.yidu.hbatsft.domain.AtsAttachment;
import com.yidu.hbatsft.domain.AtsAttachmentCondition;
import com.yidu.hbatsft.domain.AtsFileData;
import com.yidu.hbatsft.service.AtsAttachmentService;
import com.yidu.utils.FileTools;
import com.yidu.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * ClassName:AtsAttachmentController
 * Package:com.yidu.hbatsft.action
 * Description: 辅助功能测试版
 *
 * @Date 2019-11-04 10:31
 * @Author 陈熠
 */
@Controller
@RequestMapping("/attachment")
public class AtsAttachmentController {

    @Resource
    AtsAttachmentService atsAttachmentService;
    @Resource
    FileTools fileTools;

    /**
     * 根据附件id查询编辑功能所需要的用户数据
     * 编辑功能
     * @param attachId 主键id
     * @return 查询对象
     */
    @ResponseBody
    @RequestMapping("/selectByPrimaryKey")
    public Map<String, Object> selectByPrimaryKey(Integer attachId){
        Map<String, Object> map = new HashMap<String, Object>();
        //通过附件id得到附件对象
        AtsAttachment atsAttachment = atsAttachmentService.selectByPrimaryKey(attachId);

        String fileType = atsAttachment.getAttachType();
        String fileTypeId = String.valueOf(atsAttachment.getAttachId());
        AtsFileData atsFileData = new AtsFileData();
        atsFileData.setFileType(fileType);
        atsFileData.setFileTypeId(fileTypeId);

        //通过附件id，附件类型，得到文件集合
        List<AtsFileData> list1 = atsAttachmentService.selectAttachFileAll(atsFileData);
        //查询所有用户，得到发布对象树形控件所需要的数据
        List<Map<String, Object>> treeUsers = atsAttachmentService.findAllUsers();
        //查询已发布的对象，用于选中树形控件
        String[] checkedUser = atsAttachmentService.findUserIdsByAttachId(attachId);
        map.put("checkedUser",checkedUser);
        map.put("treeUsers",treeUsers);
        map.put("fileMessage",list1);
        map.put("data",atsAttachment);
        map.put("code",0);

        return map;
    }

    /**
     * 根据attachId查询展示页面所需要的数据
     * 浏览功能
     * @param attachId 附件表id
     * @return 数据
     */
    @ResponseBody
    @RequestMapping("/selectByAttachIdShow")
    public Map<String, Object> selectByAttachIdShow(Integer attachId){

        Map<String, Object> map = new HashMap<String, Object>();
        //通过附件id得到附件对象
        AtsAttachment atsAttachment = atsAttachmentService.selectByPrimaryKey(attachId);

        String fileType = atsAttachment.getAttachType();
        String fileTypeId = String.valueOf(atsAttachment.getAttachId());
        AtsFileData atsFileData = new AtsFileData();
        atsFileData.setFileType(fileType);
        atsFileData.setFileTypeId(fileTypeId);

        //通过附件id，附件类型，得到文件集合
        List<AtsFileData> list1 = atsAttachmentService.selectAttachFileAll(atsFileData);
        map.put("fileMessage",list1);
        map.put("data",atsAttachment);
        map.put("code",0);

        return map;
    }

    /**
     * 新增功能发布对象需要的员工树形数据
     * 树形控件数据
     * @return 公共数据
     */
    @ResponseBody
    @RequestMapping("/treeUsers")
    public Map<String, Object> treeUsers(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> treeUsers = atsAttachmentService.findAllUsers();
        map.put("treeUsers",treeUsers);
        return map;
    }

    /**
     * 根据用户id关联查询附件关联表和附件表
     * 查看页面
     * @param attachmentCondition 文件类型，查询条件
     * @return 行数据集合，状态，总页数
     */
    @ResponseBody
    @RequestMapping("/selectTypeAll")
    public Map<String, Object> selectTypeAll(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<String, Object>();
        List<AtsAttachment> list = atsAttachmentService.selectTypeAll(attachmentCondition);
        int count = atsAttachmentService.selectCount(attachmentCondition);

        map.put("count",count);
        map.put("data",list);
        map.put("code",0);
        return map;
    }

    /**
     *  查询首页所需要的附件信息
     *  首页展示页面
     * @param attachmentCondition 用户id，分页信息，附件类别
     * @return 附件信息
     */
    @ResponseBody
    @RequestMapping("/selectHomePageByUserId")
    public Map<String,Object> selectHomePageByUserId(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<String, Object>();

        attachmentCondition.setCreater(Integer.valueOf(attachmentCondition.getUserId()));
        List<AtsAttachment> list = atsAttachmentService.selectHomePageByUserId(attachmentCondition);

        map.put("data",list);
        map.put("code",0);
        return map;
    }


    /**
     * 根据用户id查询用户所编写的附件表
     * 编辑页面
     * @param attachmentCondition 用户id，查询条件
     * @return 行数据集合，状态，总页数
     */
    @ResponseBody
    @RequestMapping("/selectByUserId")
    public Map<String, Object> selectByUserId(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<String, Object>();

        attachmentCondition.setCreater(Integer.valueOf(attachmentCondition.getUserId()));
        List<AtsAttachment> list = atsAttachmentService.selectByUserId(attachmentCondition);
        int count = atsAttachmentService.selectByUserIdCount(attachmentCondition);

        map.put("count",count);
        map.put("data",list);
        map.put("code",0);
        return map;
    }


    /**
     * 根据附件表类型查询所用的附件表
     * 审核功能
     * @param attachmentCondition 条件
     * @return 行数据集合，状态，总页数
     */
    @ResponseBody
    @RequestMapping("/selectByAttachType")
    public Map<String, Object> selectByAttachType(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<String, Object>();

        attachmentCondition.setCreater(Integer.valueOf(attachmentCondition.getUserId()));
        List<AtsAttachment> list = atsAttachmentService.selectByAttachType(attachmentCondition);
        int count = atsAttachmentService.selectByAttachTypeCount(attachmentCondition);

        map.put("count",count);
        map.put("data",list);
        map.put("code",0);
        return map;
    }

    /**
     * 附件删除
     * 删除功能
     * @param attachmentCondition 条件
     * @return 状态：1为成功，0为失败
     */
    @ResponseBody
    @RequestMapping("/attachDelete")
    public Map<String, Object> attachDelete(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<String, Object>();
        int state = atsAttachmentService.deleteByAttachId(attachmentCondition);
        //int state = 1;
        map.put("state",state);
        map.put("code",0);
        return map;
    }

    /**
     * 新增附件对象
     * 新增功能
     * @param attachment 新增对象
     * @return 新增结果
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/addAttachment")
    public Map<String, Object> addAttachment(AtsAttachment attachment, AtsAttachmentCondition condition) throws IOException {

        Map<String, Object> map = new HashMap<String, Object>();

        AtsAttachmentCondition attachmentCondition = atsAttachmentService.insertSelective(attachment,condition);

        map.put("fileMessage",attachmentCondition);

        return map;
    }

    /**
     * 根据附件id修改附件对象
     * 修改功能
     * @param attachment 修改对象
     * @return 修改结果
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/updateByPrimaryKeySelective")
    public Map<String, Object> updateByPrimaryKeySelective(AtsAttachment attachment, AtsAttachmentCondition condition) throws IOException {

        Map<String, Object> map = new HashMap<String, Object>();
        AtsAttachmentCondition attachmentCondition = atsAttachmentService.updateByPrimaryKeySelective(attachment,condition);
        map.put("fileMessage", attachmentCondition);

        return map;
    }

    /**
     * 文件新增
     * 新增功能
     * @param atsFileData 文件对象
     * @return 文件名和新增状态
     */
    @ResponseBody
    @RequestMapping("/insertFileData")
    public Map<String, Object> insertFileData(@RequestParam(value = "fileData", required = false) MultipartFile file,
                                              AtsFileData atsFileData) throws Exception{
        Map<String, Object> map = new HashMap<>();

        if (!Tools.isEmpty(file.getOriginalFilename())){
            double fileSize = (double)file.getSize()/1024;
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println(df.format(fileSize));
            String size = df.format(fileSize) + "kb";
            //设置文件大小
            atsFileData.setFileSize(size);
            //原文件名
            String oriFileName = file.getOriginalFilename();
            //设置文件名
            atsFileData.setFileName(oriFileName);
            String fileSite = fileTools.saveFile("function",file);
            //设置文件地址
            atsFileData.setFileSite(fileSite);
            //文件后缀
            String hz = oriFileName.split("\\.")[1];
            //设置后缀
            atsFileData.setFileSuffix(hz);
            //初始文件状态 文件编译状态，0为未编译，1为不能编译，2为图片，3为文档，4为视频
            atsFileData.setFileCompile("0");
            //word文档转pdf

        }

        AtsAttachmentCondition attachmentCondition = atsAttachmentService.insertFileData(atsFileData);

        map.put("fileMessage", attachmentCondition);

        return map;
    }

    /**
     * 对未编译文件进行编译
     * 格式转化
     * @return 编译状态和编译地址
     */
    @ResponseBody
    @RequestMapping("/compileFile")
    public Map<String, Object> compileFile(AtsFileData atsFileData){
        Map<String, Object> map = new HashMap<>();
        AtsFileData atsFileData1 = atsAttachmentService.fileCompile(atsFileData);
        map.put("fileMessage",atsFileData1);

       return map;

    }

    /**
     * 文件修改
     * 修改功能
     * @param atsFileData 修改文件对象
     * @return 修改名称和状态
     */
    @ResponseBody
    @RequestMapping("/updateFileData")
    public Map<String, Object> updateFileData(@RequestParam(value = "fileData", required = false) MultipartFile file,
                                              AtsFileData atsFileData) throws Exception{
        Map<String, Object> map = new HashMap<>();
        if (!Tools.isEmpty(file.getOriginalFilename())){
            atsFileData.setFileName(file.getOriginalFilename());
            String fileSite = fileTools.saveFile("function",file);
            atsFileData.setFileSite(fileSite);
        }

        AtsAttachmentCondition attachmentCondition = atsAttachmentService.updateFileData(atsFileData);

        map.put("data", attachmentCondition);
        return  map;
    }

    /**
     * 删除文件
     * 删除功能
     * @param atsFileData 删除对象
     * @return 删除状态
     */
    @ResponseBody
    @RequestMapping("/deleteFileData")
    public Map<String, Object> deleteFileData(AtsFileData atsFileData){
        Map<String, Object> map = new HashMap<>();

        AtsAttachmentCondition attachmentCondition = atsAttachmentService.deleteFileData(atsFileData);
        map.put("fileMessage", attachmentCondition);
        return map;
    }

    /**
     *  审核表新增
     *  新增功能
     * @param atsAttachAudit 新增数据
     * @return 新增状态
     */
    @ResponseBody
    @RequestMapping("/insertAudit")
    public Map<String, Object> insertAudit(AtsAttachAudit atsAttachAudit){
        Map<String, Object> map = new HashMap<>();
        int auditState = atsAttachmentService.insertAudit(atsAttachAudit);
        map.put("auditMessage", auditState);
        return map;
    }


    /**
     * 根据附件id查询附件信息
     * 查询功能
     * @param attachmentCondition 附件id
     * @return 附件信息
     */
    @ResponseBody
    @RequestMapping("/selectStateByAttachId")
    public Map<String, Object> selectStateByAttachId(AtsAttachmentCondition attachmentCondition){
        Map<String, Object> map = new HashMap<>();

        AtsAttachment atsAttachment = atsAttachmentService.selectByPrimaryKey(Integer.valueOf(attachmentCondition.getAuditAttachId()));

        map.put("attachment",atsAttachment);
        return map;
    }

    /**
     *  根据附件id查询审核信息
     * @param atsAttachAudit 附件id
     * @return 审核集合
     */
    @ResponseBody
    @RequestMapping("/selectByAttachId")
    public Map<String, Object> selectByAttachId(AtsAttachmentCondition atsAttachAudit){
        Map<String, Object> map = new HashMap<>();
        List<AtsAttachAudit> list = atsAttachmentService.selectByAttachId(atsAttachAudit);
        int page = atsAttachmentService.selectByAttachIdCount(atsAttachAudit);
        map.put("data", list);
        map.put("count",page);
        map.put("code",0);
        return map;
    }
}